package com.bcbim.api_web.space.controller;

import com.alibaba.fastjson.JSONObject;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.ParamException;
import com.bcbim.common.util.ParamCheck;
import com.bcbim.operation_maintenance_interface.department.dto.DepartmentDTO;
import com.bcbim.operation_maintenance_interface.space_occupy.dto.SpaceOccupyDTO;
import com.bcbim.operation_maintenance_interface.space_occupy.service.SpaceOccupyService;
import com.bcbim.operation_maintenance_interface.space_occupy.vo.SpaceStaffOccupyVO;
import com.bcbim.operation_maintenance_interface.space_type.dto.SpaceTypeDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 空间占用controller
 * @Author lihonggang
 * @Date 2022/04/28  14:04
 * @Version V1.0
 **/
@RestController
@RequestMapping("/spaceOccupy")
public class SpaceOccupyController {

    @Reference(protocol = "dubbo", group = "spaceOccupy", version = "1.0.0")
    private SpaceOccupyService spaceOccupyServiceImpl;
    /**
     * @Description:  空间占用人员列表
     * @Date:  2022/4/28 14:39
     * @Param: [spaceOccupyDTO]
     * @return: com.bcbim.common.response.vo.ReturnPageMsg
     * @Author: lihonggang
     **/
    @RequestMapping("/listSpaceStaffOccupy")
    public ReturnMsg listSpaceStaffOccupy(SpaceOccupyDTO spaceOccupyDTO) {
        //参数校验
        try {
            ParamCheck.pageNumCheck(spaceOccupyDTO.getPageNum(), spaceOccupyDTO.getPageSize());
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //整理筛选条件
        if (StringUtils.isNotEmpty(spaceOccupyDTO.getSpaceTypeJson())) {
            List<SpaceTypeDTO> typeDTOList = JSONObject.parseArray(spaceOccupyDTO.getSpaceTypeJson(), SpaceTypeDTO.class);
            spaceOccupyDTO.setSpaceTypeIds(typeDTOList);
        }
        if (StringUtils.isNotEmpty(spaceOccupyDTO.getDepartmentJson())) {
            List<DepartmentDTO> departmentDTOList = JSONObject.parseArray(spaceOccupyDTO.getDepartmentJson(), DepartmentDTO.class);
            spaceOccupyDTO.setDepartmentIds(departmentDTOList);
        }
        List<SpaceStaffOccupyVO> list = new ArrayList<>();
        //查询空间占用人员列表总数
        int total = spaceOccupyServiceImpl.listSpaceStaffOccupyCount(spaceOccupyDTO);
        if (total > 0) {
            //总数大于零,查询列表
            spaceOccupyDTO.setStart((spaceOccupyDTO.getPageNum() - 1) * spaceOccupyDTO.getPageSize());
            list = spaceOccupyServiceImpl.listSpaceStaffOccupy(spaceOccupyDTO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("staffSum", total);
        return ReturnMsg.consSuccess(map);
    }
}
